<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/xizhongren8980/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/xizhongren8980/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/xizhongren8980/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/xizhongren8980/images/logo.svg" color="#222">

<link rel="stylesheet" href="/xizhongren8980/css/main.css">


<link rel="stylesheet" href="/xizhongren8980/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/xizhongren8980/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="题目描述:给出一个区间的集合，请合并所有重叠的区间。">
<meta property="og:type" content="article">
<meta property="og:title" content="【LeetCode】56题之合并区间">
<meta property="og:url" content="https://gitee.com/xizhongren8980/xizhongren8980.git/2020/04/16/56%E9%A2%98%E4%B9%8B%E5%90%88%E5%B9%B6%E5%8C%BA%E9%97%B4/index.html">
<meta property="og:site_name" content="一杯茶一包烟，一个BUG改一天">
<meta property="og:description" content="题目描述:给出一个区间的集合，请合并所有重叠的区间。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/xizhongren8980/Hexo_src/raw/master/LeetCode/56_1.png">
<meta property="og:image" content="https://gitee.com/xizhongren8980/Hexo_src/raw/master/LeetCode/56_result.png">
<meta property="article:published_time" content="2020-04-16T08:52:26.492Z">
<meta property="article:modified_time" content="2020-04-16T09:11:47.254Z">
<meta property="article:author" content="MisterTian">
<meta property="article:tag" content="栈">
<meta property="article:tag" content="排序问题">
<meta property="article:tag" content="快排">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/xizhongren8980/Hexo_src/raw/master/LeetCode/56_1.png">

<link rel="canonical" href="https://gitee.com/xizhongren8980/xizhongren8980.git/2020/04/16/56%E9%A2%98%E4%B9%8B%E5%90%88%E5%B9%B6%E5%8C%BA%E9%97%B4/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>【LeetCode】56题之合并区间 | 一杯茶一包烟，一个BUG改一天</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/xizhongren8980/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">一杯茶一包烟，一个BUG改一天</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">收藏永不停止，学习永不开始</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/xizhongren8980/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/xizhongren8980/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/xizhongren8980/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/xizhongren8980/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/xizhongren8980/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/xizhongren8980/xizhongren8980.git/2020/04/16/56%E9%A2%98%E4%B9%8B%E5%90%88%E5%B9%B6%E5%8C%BA%E9%97%B4/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/xizhongren8980/images/head.jpg">
      <meta itemprop="name" content="MisterTian">
      <meta itemprop="description" content="一个小菜鸟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一杯茶一包烟，一个BUG改一天">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          【LeetCode】56题之合并区间
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-04-16 16:52:26 / 修改时间：17:11:47" itemprop="dateCreated datePublished" datetime="2020-04-16T16:52:26+08:00">2020-04-16</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/xizhongren8980/categories/LeetCode/" itemprop="url" rel="index"><span itemprop="name">LeetCode</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h4 id="题目描述"><a href="#题目描述" class="headerlink" title="题目描述:"></a>题目描述:</h4><p>给出一个区间的集合，请合并所有重叠的区间。<a id="more"></a></p>
<h4 id="示例："><a href="#示例：" class="headerlink" title="示例："></a>示例：</h4><pre>
输入: [[1,3],[2,6],[8,10],[15,18]]
输出: [[1,6],[8,10],[15,18]]
解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6]
</pre>


<pre>
输入: [[1,4],[4,5]]
输出: [[1,5]]
解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间
</pre>


<h4 id="分析："><a href="#分析：" class="headerlink" title="分析："></a>分析：</h4><p>首先题目没有说明这个区间集合是有序的，所以为了更好对区间进行合并，首先要做的就是对给出的区间集合进行排序。我是按照区间的开始大小来进行排序的。</p>
<p>区间集合有序后再进行合并就变得很简单了，我们只需要对区间从头到尾进行一次次的合并即可。合并的时候有以下几种情况：</p>
<p><img src="https://gitee.com/xizhongren8980/Hexo_src/raw/master/LeetCode/56_1.png" alt=""></p>
<p>合并的时候借助栈来存储已合并的区间集合，每次加入新的区间，只需要与栈顶和次栈顶元素进行比较即可。</p>
<p>当全部区间集合都存储在栈中，我们就可以两两为一组区间取出来。</p>
<h4 id="Java实现"><a href="#Java实现" class="headerlink" title="Java实现"></a>Java实现</h4><pre>
<figure class="highlight angelscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="symbol">Solution</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">int</span>[][] merge(<span class="built_in">int</span>[][] <span class="built_in">int</span>ervals) &#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="built_in">int</span>ervals.length&gt;<span class="number">0</span>)&#123;</span><br><span class="line">            Stack&lt;Integer&gt; stack1 = new Stack&lt;&gt;();</span><br><span class="line">            <span class="built_in">int</span> row = <span class="built_in">int</span>ervals.length;</span><br><span class="line">            </span><br><span class="line">            <span class="comment">//按区间的开始部分递增对二维数组进行排序</span></span><br><span class="line">            <span class="comment">//sort(intervals);</span></span><br><span class="line">            sort_quick(<span class="built_in">int</span>ervals, <span class="number">0</span>, row<span class="number">-1</span>);</span><br><span class="line"></span><br><span class="line">            stack1.push(<span class="built_in">int</span>ervals[<span class="number">0</span>][<span class="number">0</span>]);       <span class="comment">//初始化栈</span></span><br><span class="line">            stack1.push(<span class="built_in">int</span>ervals[<span class="number">0</span>][<span class="number">1</span>]);</span><br><span class="line">            <span class="keyword">for</span>(<span class="built_in">int</span> i = <span class="number">1</span>; i &lt; row; i++)&#123;</span><br><span class="line">                <span class="keyword">if</span>(<span class="built_in">int</span>ervals[i][<span class="number">0</span>] &lt;= stack1.peek() &amp;&amp; <span class="built_in">int</span>ervals[i][<span class="number">1</span>] &gt;= stack1.peek())&#123;</span><br><span class="line">                    stack1.pop();</span><br><span class="line">                    <span class="keyword">if</span>(<span class="built_in">int</span>ervals[i][<span class="number">0</span>] &lt; stack1.peek())&#123;</span><br><span class="line">                        stack1.pop();</span><br><span class="line">                        stack1.push(<span class="built_in">int</span>ervals[i][<span class="number">0</span>]);</span><br><span class="line">                        stack1.push(<span class="built_in">int</span>ervals[i][<span class="number">1</span>]);</span><br><span class="line">                    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                        stack1.push(<span class="built_in">int</span>ervals[i][<span class="number">1</span>]);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="built_in">int</span>ervals[i][<span class="number">0</span>] &lt;= stack1.peek() &amp;&amp; <span class="built_in">int</span>ervals[i][<span class="number">1</span>] &lt; stack1.peek())&#123;</span><br><span class="line">                    <span class="built_in">int</span> temp = stack1.pop();</span><br><span class="line">                    <span class="keyword">if</span>(<span class="built_in">int</span>ervals[i][<span class="number">0</span>] &lt; stack1.peek() &amp;&amp; <span class="built_in">int</span>ervals[i][<span class="number">1</span>] &lt;= stack1.peek())&#123;</span><br><span class="line">                        <span class="keyword">if</span>(<span class="built_in">int</span>ervals[i][<span class="number">1</span>] == stack1.peek())&#123;</span><br><span class="line">                            stack1.pop();</span><br><span class="line">                            stack1.push(<span class="built_in">int</span>ervals[i][<span class="number">0</span>]);</span><br><span class="line">                        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                            <span class="built_in">int</span> t = stack1.pop();</span><br><span class="line">                            stack1.push(<span class="built_in">int</span>ervals[i][<span class="number">0</span>]);</span><br><span class="line">                            stack1.push(<span class="built_in">int</span>ervals[i][<span class="number">1</span>]);</span><br><span class="line">                            stack1.push(t);</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">if</span>(<span class="built_in">int</span>ervals[i][<span class="number">0</span>] &lt; stack1.peek() &amp;&amp; <span class="built_in">int</span>ervals[i][<span class="number">1</span>] &gt; stack1.peek())&#123;</span><br><span class="line">                        stack1.pop();</span><br><span class="line">                        stack1.push(<span class="built_in">int</span>ervals[i][<span class="number">0</span>]);</span><br><span class="line">                    &#125;</span><br><span class="line">                    stack1.push(temp);</span><br><span class="line">                &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="built_in">int</span>ervals[i][<span class="number">0</span>] &gt; stack1.peek())&#123;</span><br><span class="line">                    stack1.push(<span class="built_in">int</span>ervals[i][<span class="number">0</span>]);</span><br><span class="line">                    stack1.push(<span class="built_in">int</span>ervals[i][<span class="number">1</span>]);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            List&lt;Integer&gt; list = new ArrayList&lt;&gt;();</span><br><span class="line">            <span class="keyword">while</span>(!stack1.isEmpty())&#123;</span><br><span class="line">                list.add(stack1.pop());</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="built_in">int</span> list_length = list.size();</span><br><span class="line">            <span class="built_in">int</span>[][] a = new <span class="built_in">int</span>[list_length/<span class="number">2</span>][<span class="number">2</span>];</span><br><span class="line">            <span class="keyword">for</span>(<span class="built_in">int</span> m = <span class="number">0</span>; m &lt; list_length/<span class="number">2</span>; m++)&#123;</span><br><span class="line">                <span class="keyword">for</span>(<span class="built_in">int</span> n = <span class="number">0</span>; n &lt; <span class="number">2</span>; n++)&#123;</span><br><span class="line">                    a[m][n] = list.remove(list.size()<span class="number">-1</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> a;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="built_in">int</span>[][] a = new <span class="built_in">int</span>[<span class="number">0</span>][<span class="number">0</span>];</span><br><span class="line">            <span class="keyword">return</span> a;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;  </span><br><span class="line">    <span class="comment">//冒泡排序</span></span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">int</span>[][] sort(<span class="built_in">int</span>[][] <span class="built_in">array</span>) &#123;</span><br><span class="line">        <span class="built_in">int</span> i, j = <span class="number">0</span>;</span><br><span class="line">        <span class="built_in">int</span> temp1, temp2 = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="built_in">array</span>.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (j = <span class="number">1</span>; j &lt; <span class="built_in">array</span>.length - i; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="built_in">array</span>[j - <span class="number">1</span>][<span class="number">0</span>] &gt; <span class="built_in">array</span>[j][<span class="number">0</span>]) &#123;</span><br><span class="line">                    temp1 = <span class="built_in">array</span>[j - <span class="number">1</span>][<span class="number">0</span>];</span><br><span class="line">                    temp2 = <span class="built_in">array</span>[j - <span class="number">1</span>][<span class="number">1</span>];</span><br><span class="line">                    <span class="built_in">array</span>[j - <span class="number">1</span>][<span class="number">0</span>] = <span class="built_in">array</span>[j][<span class="number">0</span>];</span><br><span class="line">                    <span class="built_in">array</span>[j - <span class="number">1</span>][<span class="number">1</span>] = <span class="built_in">array</span>[j][<span class="number">1</span>];</span><br><span class="line">                    <span class="built_in">array</span>[j][<span class="number">0</span>] = temp1;</span><br><span class="line">                    <span class="built_in">array</span>[j][<span class="number">1</span>] = temp2;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">array</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//快速排序</span></span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">void</span> sort_quick(<span class="built_in">int</span> a[][], <span class="built_in">int</span> start, <span class="built_in">int</span> end) &#123;</span><br><span class="line">        <span class="built_in">int</span> low = start;</span><br><span class="line">        <span class="built_in">int</span> high = end;</span><br><span class="line">        <span class="built_in">int</span> key = a[start][<span class="number">0</span>];</span><br><span class="line">        <span class="keyword">while</span> (high &gt; low) &#123;</span><br><span class="line">            <span class="comment">//从后往前扫描</span></span><br><span class="line">            <span class="keyword">while</span> (high &gt; low &amp;&amp; a[high][<span class="number">0</span>] &gt;= key) &#123;</span><br><span class="line">                high--;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (a[high][<span class="number">0</span>] &lt;= key) &#123;</span><br><span class="line">                <span class="built_in">int</span> t1 = a[high][<span class="number">0</span>];</span><br><span class="line">                <span class="built_in">int</span> t2 = a[high][<span class="number">1</span>];</span><br><span class="line">                a[high][<span class="number">0</span>] = a[low][<span class="number">0</span>];</span><br><span class="line">                a[high][<span class="number">1</span>] = a[low][<span class="number">1</span>];</span><br><span class="line">                a[low][<span class="number">0</span>] = t1;</span><br><span class="line">                a[low][<span class="number">1</span>] = t2;</span><br><span class="line">             &#125;</span><br><span class="line">            <span class="comment">//从前往后扫描</span></span><br><span class="line">            <span class="keyword">while</span> (high &gt; low &amp;&amp; a[low][<span class="number">0</span>] &lt;= key) &#123;</span><br><span class="line">                low++;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (a[low][<span class="number">0</span>] &gt;= key) &#123;</span><br><span class="line">                <span class="built_in">int</span> t1 = a[low][<span class="number">0</span>];</span><br><span class="line">                <span class="built_in">int</span> t2 = a[low][<span class="number">1</span>];</span><br><span class="line">                a[low][<span class="number">0</span>] = a[high][<span class="number">0</span>];</span><br><span class="line">                a[low][<span class="number">1</span>] = a[high][<span class="number">1</span>];</span><br><span class="line">                a[high][<span class="number">0</span>] = t1;</span><br><span class="line">                a[high][<span class="number">1</span>] = t2;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//对左边序列进行递归排序</span></span><br><span class="line">        <span class="keyword">if</span> (low &gt; start) &#123;</span><br><span class="line">            sort_quick(a, start, low - <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//对右边序列进行递归排序</span></span><br><span class="line">        <span class="keyword">if</span> (high &lt; end) &#123;</span><br><span class="line">            sort_quick(a, high + <span class="number">1</span>, end);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</pre>


<h4 id="运行结果："><a href="#运行结果：" class="headerlink" title="运行结果："></a>运行结果：</h4><p><img src="https://gitee.com/xizhongren8980/Hexo_src/raw/master/LeetCode/56_result.png" alt=""></p>
<h4 id="优化："><a href="#优化：" class="headerlink" title="优化："></a>优化：</h4><p>采用好的排序算法可以明显提升执行效率。</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/xizhongren8980/tags/%E6%A0%88/" rel="tag"># 栈</a>
              <a href="/xizhongren8980/tags/%E6%8E%92%E5%BA%8F%E9%97%AE%E9%A2%98/" rel="tag"># 排序问题</a>
              <a href="/xizhongren8980/tags/%E5%BF%AB%E6%8E%92/" rel="tag"># 快排</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/xizhongren8980/2020/04/15/542%E9%A2%98%E4%B9%8B01%E7%9F%A9%E9%98%B5/" rel="prev" title="【LeetCode】542题之01矩阵">
      <i class="fa fa-chevron-left"></i> 【LeetCode】542题之01矩阵
    </a></div>
      <div class="post-nav-item">
    <a href="/xizhongren8980/2020/04/17/55%E9%A2%98%E4%B9%8B%E8%B7%B3%E8%B7%83%E6%B8%B8%E6%88%8F/" rel="next" title="【LeetCode】55题之跳跃游戏">
      【LeetCode】55题之跳跃游戏 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#题目描述"><span class="nav-number">1.</span> <span class="nav-text">题目描述:</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#示例："><span class="nav-number">2.</span> <span class="nav-text">示例：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#分析："><span class="nav-number">3.</span> <span class="nav-text">分析：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Java实现"><span class="nav-number">4.</span> <span class="nav-text">Java实现</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#运行结果："><span class="nav-number">5.</span> <span class="nav-text">运行结果：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#优化："><span class="nav-number">6.</span> <span class="nav-text">优化：</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="MisterTian"
      src="/xizhongren8980/images/head.jpg">
  <p class="site-author-name" itemprop="name">MisterTian</p>
  <div class="site-description" itemprop="description">一个小菜鸟</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/xizhongren8980/archives/">
        
          <span class="site-state-item-count">57</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/xizhongren8980/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/xizhongren8980/tags/">
          
        <span class="site-state-item-count">101</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://mistertian8980.github.io/" title="https:&#x2F;&#x2F;mistertian8980.github.io&#x2F;" rel="noopener" target="_blank">Github博客</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">MisterTian</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script size="300" alpha="0.6" zIndex="-1" src="/xizhongren8980/lib/canvas-ribbon/canvas-ribbon.js"></script>
  <script src="/xizhongren8980/lib/anime.min.js"></script>
  <script src="/xizhongren8980/lib/velocity/velocity.min.js"></script>
  <script src="/xizhongren8980/lib/velocity/velocity.ui.min.js"></script>

<script src="/xizhongren8980/js/utils.js"></script>

<script src="/xizhongren8980/js/motion.js"></script>


<script src="/xizhongren8980/js/schemes/pisces.js"></script>


<script src="/xizhongren8980/js/next-boot.js"></script>


  <script defer src="//cdn.jsdelivr.net/gh/theme-next/theme-next-three@1/three.min.js"></script>
    <script defer src="//cdn.jsdelivr.net/gh/theme-next/theme-next-three@1/canvas_lines.min.js"></script>


  




  
<script src="/xizhongren8980/js/local-search.js"></script>













  

  

</body>
</html>
